// based on Fast_rand from Listing 1 at:
// http://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/

package oboe.utils
{
	public class RandomInt
	{
		private static var seed:uint=0;
		
		public static function srand( nseed:uint ):int
		{
			seed = (214013*nseed+2531011); 
			return (seed>>16)&0x7FFF; 
		}
		
		public static function rand():int
		{
			seed = (214013*seed+2531011); 
			return (seed>>16)&0x7FFF; 
		}

		public static function srandf( nseed:Number ):Number
		{
			seed = (214013*int(nseed*1000)+2531011); 
			return ((seed>>16)&0x7FFF)/32767.0; 
		}
		
		public static function randf():Number
		{
			seed = (214013*seed+2531011); 
			return ((seed>>16)&0x7FFF)/32767.0; 
		}

		private static var pool:Vector.<Number>;
		public static function tsrandf( nseed:int ):Number
		{
			if( !pool )
			{
				pool = new Vector.<Number>(0x40, true);
				var tseed:uint = (214013*int(seed)+2531011);
				for( var i:int=0; i<0x40; ++i )
				{
					tseed = (tseed>>16)&0x7FFF; 
					pool[ i ] = tseed / 32767.0;
					tseed = (214013*int(tseed)+2531011); 
				}
			}
			return pool[ nseed&0x3F ];
		}
	}
}
